from data.Datagrama import SimpleDatagram, IPDatagram
import re

def mountSimpleDatagram(line):
    data = re.split(" ", line )
    
    layer = data[1].strip()
    
    if len(data) <= 4:
        return SimpleDatagram(data[0],layer,"null","null","null","null")    
    else:
        s_ip , s_port = getIpPort(data[2])
        d_ip , d_port = getIpPort(data[4])
        
        
        if layer == "IP":
            if data[5] == "":
                return IPDatagram(data[0],layer,s_ip,s_port.split(":")[0],d_ip,d_port.split(":")[0],"UDP")
            else:
                return IPDatagram(data[0],layer,s_ip,s_port.split(":")[0],d_ip,d_port.split(":")[0],"TCP")
            
        return SimpleDatagram(data[0],layer,s_ip,s_port.split(":")[0],d_ip,d_port.split(":")[0])

def getIpPort(data):
    s_port = re.split("\.", data)
    s_port = s_port[len(s_port)-1]
    return data[0:(len(data)-len(s_port))-1] , s_port

def getImportantPorts(ports_file):
    f = open(ports_file,"r")
    ports = []
    count = []
    
    for line in f:
        if line != "\n":
            data = re.split("\n", line )
            ports.append(data[0])
            count.append(0)
    return ports,count